package com.xyy.saas.payment.service.impl;

import com.xyy.saas.payment.common.constants.ChannelConstants;
import com.xyy.saas.payment.dao.mapper.PayResultOrderMapper;
import com.xyy.saas.payment.dao.model.PayResultOrder;
import com.xyy.saas.payment.service.PayResultOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class PayResultOrderServiceImpl implements PayResultOrderService {

    @Autowired
    private PayResultOrderMapper payResultOrderMapper;

    public PayResultOrder selectByPayNoAndChannel(String payNo, String channel){
        List<PayResultOrder> list = selectListByPayNo(payNo);

        if(CollectionUtils.isEmpty(list)) {
            return null;
        }

        if(list.size() == 1) {
            return list.get(0);
        }

        for(PayResultOrder payResultOrder : list) {
            if(payResultOrder.getPayChannel().equals(channel)) {
                return payResultOrder;
            }
            else {
                if(payResultOrder.getPayChannel().contains(ChannelConstants.CHANNEL_CHINA_BANK_NAME)) {
                    if(payResultOrder.getPayChannel().split("_")[1] .equals(channel.split("_")[1])) {
                        return payResultOrder;
                    }
                }

                if(payResultOrder.getPayChannel().contains(ChannelConstants.CHANNEL_FUMIN_BANK_NAME)) {
                    if(payResultOrder.getPayChannel().split("_")[1] .equals(channel.split("_")[1])) {
                        return payResultOrder;
                    }
                }

                if(payResultOrder.getPayChannel().contains(ChannelConstants.CHANNEL_YOP_BANK_NAME)) {
                    if(channel.contains(payResultOrder.getPayChannel().split("_")[1])) {
                        return payResultOrder;
                    }
                }

                if(payResultOrder.getPayChannel().contains(ChannelConstants.CHANNEL_PINGAN_NAME)) {
                    if(channel.contains(payResultOrder.getPayChannel().split("_")[1])) {
                        return payResultOrder;
                    }
                }

                if(payResultOrder.getPayChannel().contains(ChannelConstants.CHANNEL_DIRECT_NAME)) {
                    if(channel.contains(payResultOrder.getPayChannel().split("_")[1])) {
                        return payResultOrder;
                    }
                }


            }
        }

        return null;
    }

    public PayResultOrder selectByChannelChargeNo(String channelChargeNo) {
        return payResultOrderMapper.selectByChannelChargeNo(channelChargeNo);
    }

    public List<PayResultOrder> selectListByPayNo(String payNo) {
        return payResultOrderMapper.selectByPayNo(payNo);
    }

    public List<PayResultOrder> selectListByPayNos(List<String> payNos) {
        return payResultOrderMapper.selectByPayNos(payNos);
    }

    public int insertSelective(PayResultOrder payResultOrder){
        return payResultOrderMapper.insertSelective(payResultOrder);
    }

    /**
     * 根据OrderType统计个渠道商户交易金额
     * @param businessOrderType
     * @param startTime
     * @param endTime
     * @return
     */
    public List<PayResultOrder> sumAmountByOrderType(String businessOrderType, String payChannel, Date startTime, Date endTime) {
        return payResultOrderMapper.sumAmountByOrderType(businessOrderType, payChannel, startTime, endTime);
    }

    public List<PayResultOrder> queryCountAmountByParamGroup(PayResultOrder queryParam, Date startTime, Date endTime) {
        // 不给时间默认昨天
        if(null == startTime || null == endTime) {
            return new ArrayList<>();
        }
        return payResultOrderMapper.countAmountByParamGroup(queryParam, startTime, endTime);
    }

    @Override
    public int updateByPrimaryKeySelective(PayResultOrder record) {
        return payResultOrderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public PayResultOrder selectByChannelTransactionNo(String channelTransactionNo) {
        return payResultOrderMapper.selectByChannelTransactionNo(channelTransactionNo);
    }

    @Override
    public List<PayResultOrder> queryByChannelTransactionNo(List<String> list) {
        return payResultOrderMapper.queryByChannelTransactionNo(list);
    }

    @Override
    public List<PayResultOrder> queryNoneRoyaltyList(LocalDateTime beginTime, LocalDateTime endTime) {
        if (beginTime == null || endTime == null) {
            return Collections.emptyList();
        }
        return payResultOrderMapper.queryNoneRoyaltyList(beginTime, endTime);
    }

    @Override
    public List<PayResultOrder> queryPinganCreditListByTime(LocalDateTime beginTime, LocalDateTime endTime) {
        if (beginTime == null || endTime == null) {
            return Collections.emptyList();
        }
        return payResultOrderMapper.queryPinganCreditListByTime(beginTime, endTime);
    }
}
